//
//  ModifyPasswordViewController.m
//  parent
//
//  Created by IreWesT on 15/5/29.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "Config.h"

@interface ModifyPasswordViewController ()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UITextField *oldField;
@property (nonatomic, retain) UITextField *otherField;
@property (nonatomic, retain) UITextField *confirmField;

@end

@implementation ModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT -  64.0f)];
    _scrollView.backgroundColor = UIColorMake(244, 244, 244);
    [self.view addSubview:_scrollView];
    [self CustomBackButton];
    self.navigationItem.title = @"修改密码";
    [self createNavRightButton:@"确定" target:self action:@selector(pubPassword)];
    UIColor *titleColor = UIColorMake(173, 173, 173);
    CGFloat height = 44.0f;
    NSArray *titles = @[@"旧密码",@"新密码",@"重复新密码"];
    
    for (int i = 0; i < 3; i++) {
        UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(0, 22 + i * height, APP_SCREEN_WIDTH, height)];
        cell.backgroundColor = [UIColor whiteColor];
        [_scrollView addSubview:cell];
        
        CALayer *border = [CALayer layer];
        border.backgroundColor = DefaultBorderColor.CGColor;
        border.frame = CGRectMake(0, cell.height - .5f, cell.width, .5f);
        [cell.layer addSublayer:border];
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, height)];
        title.backgroundColor = [UIColor clearColor];
        title.textColor = titleColor;
        title.font = [UIFont systemFontOfSize:16.0f];
        title.text = titles[i];
        [cell addSubview:title];
        
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(title.right + 10, 0, cell.width - title.right - 10 - 15, height)];
        field.secureTextEntry = YES;
        field.returnKeyType = UIReturnKeyDone;
        field.clearButtonMode = UITextFieldViewModeWhileEditing;
        [cell addSubview:field];
        if (i == 0) {
            _oldField = field;
            [field becomeFirstResponder];
        }else if (i == 1) {
            _otherField = field;
        }else {
            _confirmField = field;
        }
    }
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20 , 44, 44)];
//    button.showsTouchWhenHighlighted = YES;
//    button.backgroundColor = [UIColor clearColor];
//    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
//    [button setImage:[UIImage imageNamed:@"nav_btn_back"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
//    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
//    
//    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
//                                   initWithTitle:@""
//                                   style:UIBarButtonItemStylePlain
//                                   target:nil
//                                   action:nil];
//    self.navigationItem.backBarButtonItem=backButton;
}
-(void)backButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pubPassword {

    if (_oldField.text.length == 0) {
        [self ShowExclaHud:@"请输入旧密码"];
        [_oldField becomeFirstResponder];
        return;
    }
    
    if ([_otherField.text containsString:@" "]) {
        [self ShowExclaHud:@"密码不得包含空格"];
        [_otherField becomeFirstResponder];
        return;
    }
    
    if (_otherField.text.length < 6) {
        [self ShowExclaHud:@"密码不能少于6个字符"];
        [_otherField becomeFirstResponder];
        return ;
    }else if (_otherField.text.length > 12) {
        [self ShowExclaHud:@"密码不得超过12个字符"];
        [_otherField becomeFirstResponder];
        return ;
    }
    
    if (![_otherField.text isEqualToString:_confirmField.text]) {
        [self ShowExclaHud:@"两次密码不一致"];
        [_confirmField becomeFirstResponder];
        return;
    }
    
    [_oldField resignFirstResponder];
    [_otherField resignFirstResponder];
    [_confirmField resignFirstResponder];
    
    NSLog(@"111111");
    [self apiChangePasswordWithOldPwd:_oldField.text withNewPwd:_otherField.text];
}

#pragma mark - Networking

-(BOOL)apiChangePasswordWithOldPwd:(NSString *)oldPwd withNewPwd:(NSString *)newPwd {
    
    [super showHud:@"修改中" isDim:YES];
    
    NSString* uid = @([[Config Instance]getUID]);
    /*
    NSDictionary *temp_dict = @{@"uid":uid,
                                @"token":[[UserDao getInstance]getAccessToken],
                                @"oldpwd":oldPwd,
                                @"newpwd":newPwd};
    
    NSString *dataString = [JsonUtil convertDictionaryToJson:temp_dict];
    
    NSString *msg = @"USER_MODIFY_PWD";
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            //NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [super showHudComplete:@"修改失败" success:NO];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"%@", dict);
                if ([[dict objectForKey:@"retCode"] integerValue]== 1) {
                    
                    [super showHudComplete:@"修改成功" success:YES];
                    [self.navigationController popViewControllerAnimated:YES];
                }else if ([[dict objectForKey:@"retCode"] integerValue] == 10012){
                    [super showHudComplete:@"旧密码错误" success:NO];
                }else {
                    [super showHudComplete:@"修改失败" success:NO];
                }
            }else{
                [super showHudComplete:@"服务器返回数据失败" success:NO];
            }
        }
    }];*/
    return YES;
}

@end
